package order;

import api.OrderFeignApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pojo.Account;
import pojo.AccountAndOrder;
import pojo.Order;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderTblController implements OrderFeignApi {

    @Value("${server.port}")
    String port;

    @Override
    public List<Order> insertOrder(Long accountId, String commodityCode, Long count, Long money) {
        // 模拟给当前账户下单
        List<Order> list = new ArrayList<>();
        for (long i = 0L; i < 10000L; i++) {
            Order order = new Order();
            order.setAccountId(accountId);
            order.setCommodityCode(commodityCode);
            order.setCount(count);
            order.setMoney(1L);
            order.setPort(port);
            list.add(order);
        }
        return list;
    }

    @Override
    public Order getOrder(Order order, Long accountId) {
        System.out.println(order);
        System.out.println(accountId);
        return order;
    }

    @Override
    public List<Order> id(String id) {
        System.out.println(id);
        return null;
    }

    @Override
    public Order addOrder(Order order) {
        return order;
    }


    @Override
    public Order addOrderAnd(AccountAndOrder accountAndOrder) {
        System.out.println(accountAndOrder.getAccount());
        System.out.println(accountAndOrder.getOrder());
        return null;
    }


    @Override
    public List<Order> noParam(List<Long> list) {
        list.forEach(e->{
            System.out.println(e);
        });
        return null;
    }

    @Override
    public ResponseEntity<byte[]> download(@RequestParam("filePath") String filePath) {
        try (InputStream in = new FileInputStream(filePath)) {
            // 获取文件流
            byte[] body = new byte[in.available()];
            in.read(body); // 写入到字节数组
            String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); // 截取到文件名
            MultiValueMap<String, String> headers = new HttpHeaders(); // 将文件信息写入消息头
            headers.add("Content-Disposition", "attachment;filename=" + fileName);
            return new ResponseEntity<byte[]>(body, headers, HttpStatus.OK);
        } catch (FileNotFoundException exception) {
            exception.printStackTrace();
            return new ResponseEntity<byte[]>("服务器IOException".getBytes(), HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<byte[]>("服务器IOException".getBytes(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Override
    public Object springUpload(MultipartFile file) throws InterruptedException {
        TimeUnit.SECONDS.sleep(50);
        System.out.println(file.getOriginalFilename());
        return null;
    }
}
